from .base import ShowList
from django.shortcuts import render, redirect
from utils.pagenation import Pagination
from crm.forms import ClassForm, CourseRecordForm, StudyRecordForm
from crm import models
from django.forms import modelformset_factory


# 展示班级
class ClassList(ShowList):

    def get(self, request, *args, **kwargs):
        classes = models.ClassList.objects.all()
        page = Pagination(request.GET.get('page'), classes.count(), request.GET.copy())
        return render(request, 'teacher/class_list.html',
                      {'classes': classes[page.start: page.end], 'page_html': page.page_html})


# 增加和编辑班级
def class_change(request, pk=None):
    form_obj = models.ClassList.objects.filter(pk=pk).first()
    obj = ClassForm(instance=form_obj)
    if request.method == 'POST':
        obj = ClassForm(request.POST, instance=form_obj)
        if obj.is_valid():
            obj.save()
            next_url = request.GET.get('next')
            if next_url:
                return redirect(next_url)
            return redirect('crm:class_list')
    title = '编辑班级' if pk else '添加班级'
    return render(request, 'form.html', {'obj': obj, 'title': title})


# 展示课程记录
class CourseRecordList(ShowList):

    def get(self, request, class_id, *args, **kwargs):
        course_records = models.CourseRecord.objects.filter(re_class=class_id)
        page = Pagination(request.GET.get('page'), course_records.count(), request.GET.copy())
        return render(request, 'teacher/course_record_list.html',
                      {'course_records': course_records[page.start: page.end], 'page_html': page.page_html,
                       'class_id': class_id})

    def multi_init(self):
        course_record_list = models.CourseRecord.objects.filter(pk__in=self.request.POST.getlist('pk'))
        for course_record in course_record_list:
            students = course_record.re_class.customer_set.filter(status='studying')
            study_record_obj = []
            for student in students:
                if not models.StudyRecord.objects.filter(student=student, course_record=course_record):
                    study_record_obj.append(models.StudyRecord(student=student, course_record=course_record))
            models.StudyRecord.objects.bulk_create(study_record_obj, 10)


# 增加和编辑课程记录
def course_record_change(request, class_id=None, pk=None):
    form_obj = models.CourseRecord(re_class_id=class_id,
                                   recorder=request.user_obj) if class_id else models.CourseRecord.objects.filter(
        pk=pk).first()
    obj = CourseRecordForm(instance=form_obj)
    if request.method == 'POST':
        obj = CourseRecordForm(request.POST, instance=form_obj)
        if obj.is_valid():
            obj.save()
            next_url = request.GET.get('next')
            return redirect(next_url)

    title = '编辑班级' if pk else '添加班级'
    return render(request, 'form.html', {'obj': obj, 'title': title})


# 展示和编辑学习记录表
def study_record(request, course_record_id):
    FormSet = modelformset_factory(model=models.StudyRecord, form=StudyRecordForm, extra=0)

    obj = FormSet(queryset=models.StudyRecord.objects.filter(course_record_id=course_record_id))

    if request.method == 'POST':
        obj = FormSet(data=request.POST)
        if obj.is_valid():
            obj.save()
            next_url = request.GET.get('next')
            return redirect(next_url)

    return render(request, 'teacher/study_record.html', {'obj': obj})
